<?php
/**
 * Created by novebeta.
 * Date: 10/19/12
 * Time: 5:03 PM
 */
class WebApplicationEndBehavior extends CBehavior
{
    // Web application end's name.
    private $_endName;

    // Getter.
    // Allows to get the current -end's name
    // this way: Yii::app()->endName;
    public function getEndName()
    {
        return $this->_endName;
    }

    // Run application's end.
    public function runEnd($name)
    {
        $this->_endName = $name;

        // Attach the changeModulePaths event handler
        // and raise it.
        $this->onModuleCreate = array($this, 'changeModulePaths');
        $this->onModuleCreate(new CEvent($this->owner));

        $this->owner->run(); // Run application.
    }

    // This event should be raised when CWebApplication
    // or CWebModule instances are being initialized.
    public function onModuleCreate($event)
    {
        $this->raiseEvent('onModuleCreate', $event);
    }

    // onModuleCreate event handler.
    // A sender must have controllerPath and viewPath properties.
    protected function changeModulePaths($event)
    {
        
        $event->sender->controllerPath .= DIRECTORY_SEPARATOR.$this->_endName;
        if(app()->theme == null)
        {$event->sender->viewPath .= DIRECTORY_SEPARATOR.$this->_endName;}
        else{
            $theme = app()->theme->name;
            $event->sender->viewPath = root().DS.'themes'.DS.$theme.DS.'views'.DS.$this->_endName;
        }
    }
}